/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
NetInventory::NetInventory()
{
   REPAO(slots)=-1;
}
/******************************************************************************/
void NetInventory::del()
{
   items.del();
   REPAO(slots)=-1;
}
/******************************************************************************/
void NetInventory::compress(File &f, Bool equipped_only)
{
   if(equipped_only)
   {
      // check which items should be compressed
      Int        equipped_items=0;
      Memc<Bool> equipped; FREPA(items)equipped .New()=false; FREPA(slots)if(i!=SLOT_TEMP)if(InRange(slots[i], equipped))equipped[slots[i]]=true;
      Memc<Int> new_index; FREPA(items)new_index.New()=(equipped[i] ? equipped_items++ : -1);

      // compress
      f.putInt(equipped_items); FREPA(items)if(equipped[i])items[i].compress(f);
      FREPA(slots)if(i!=SLOT_TEMP)f.cmpIntV(InRange(slots[i], new_index) ? new_index[slots[i]] : -1);
   }else
   {
      // compress
      f.putInt(items.elms()); FREPA(items)items[i].compress(f);
      FREPA(slots)if(i!=SLOT_TEMP)f.cmpIntV(slots[i]);
   }
}
void NetInventory::decompress(File &f)
{
   del();

   REP(f.getInt())items.New().decompress(f);
   FREPA(slots)if(i!=SLOT_TEMP)f.decIntV(slots[i]);
}
/******************************************************************************/
void NetInventory::save(File &f)
{
   items.save(f);
   f<<slots;
}
Bool NetInventory::load(File &f)
{
   del();

   if(!items.load(f))return false;
   f>>slots;
   return true;
}
/******************************************************************************/
